package cn.zn.user.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.zn.base.common.utils.PasswordUtil;
import cn.zn.base.exception.CustomException;
import cn.zn.user.entity.SysUser;
import cn.zn.user.entity.SysUserRole;
import cn.zn.user.mapper.SysUserMapper;
import cn.zn.user.mapper.SysUserRoleMapper;
import cn.zn.user.service.SysUserService;
import cn.zn.user.vo.SysUserEditVo;
import cn.zn.user.vo.SysUserPageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zn-admin
 * @since 2021-01-28
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Override
    public SysUser selectUserByLoginName(String loginName) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", loginName);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public SysUser selectUserById(String userId) {
        return null;
    }

    @Override
    @Transactional
    public void updateUserRoles(String userId, SysUserEditVo vo) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        userRoleMapper.delete(queryWrapper);

        String[] roleIds = vo.getRoleIds();
        if (ArrayUtil.isNotEmpty(roleIds)) {
            for (String roleId : roleIds) {
                SysUserRole userRole = new SysUserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(roleId);
                userRole.insert();
            }
        }
    }

    @Override
    @Transactional
    public void resetUserPwd(String userId, String newPassword) {
        String newEnPassword = PasswordUtil.encryptPassword(newPassword);
        baseMapper.resetUserPwdByUserId(userId, newEnPassword);
    }

    @Override
    @Transactional
    public String updatePassword(String userName, String oldPassword, String newPassword) {
        SysUser user = baseMapper.selectUserByCode(userName);

        if (!PasswordUtil.matchesPassword(oldPassword, user.getPassword())) {
            throw new CustomException("修改密码失败，旧密码错误");
        }
        if (PasswordUtil.matchesPassword(newPassword, user.getPassword())) {
            throw new CustomException("新密码不能与旧密码相同");
        }

        String newEnPassword = PasswordUtil.encryptPassword(newPassword);
        baseMapper.resetUserPwd(userName, newEnPassword);
        return newEnPassword;
    }

    @Override
    public Object selectUserList(SysUserPageVo vo) {
        if (!vo.getPage()) {
            return baseMapper.selectUserList(vo);
        }

        Page<SysUser> page = new Page<>(vo.getCurrent(), vo.getSize());
        return baseMapper.selectUserList(page, vo);
    }

}
